/**
 * 
 */
package com.demo2do.fms.admin.visitor.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.service.authority.OrganizationService;
import com.demo2do.fms.admin.service.authority.UserService;
import com.demo2do.fms.admin.service.data.MasterDataService;
import com.demo2do.fms.admin.service.data.VehicleFeatureService;
import com.demo2do.fms.entity.authority.Branch;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.authority.User;
import com.demo2do.fms.entity.data.MasterData;
import com.demo2do.fms.entity.data.business.VehicleFeature;
import com.demo2do.fms.entity.driver.Team;
import com.demo2do.fms.entity.supplier.ware.WareHourse;

/**
 * @author downpour
 *
 */
@Component("selectionVisitor")
public class SelectionVisitor {
    
    @Autowired
    private MasterDataService MasterDataService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Branch> branchService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<WareHourse> wareHourseService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Team> teamService;
    
    @Autowired
    private VehicleFeatureService vehicleFeatureService;
    
    @Autowired
    private OrganizationService organizationService;
    
    @Autowired
    private UserService userService;
    
    /**
     * get oil type
     * 
     * @return
     */
    public List<MasterData> getOilType(){
        return MasterDataService.listOilType();
    }

    /**
     * get shipment status
     *
     * @return
     */
    public List<MasterData> getShipmentStatus(){
        return MasterDataService.listShipmentStatus();
    }
    
    /**
     * get all master_dates mapped by type
     * 
     * @return
     */
    public Map<String, List<MasterData>> getMasterData(){
        return MasterDataService.list();
    }
    
    /**
     * 
     * @return
     */
    public List<Branch> getBranch() {
        return branchService.listAll(Branch.class);
    }
    
    /**
     * 
     * @return
     */
    public List<WareHourse> getWareHourse() {
        return wareHourseService.listAll(WareHourse.class);
    }
    
    /**
     * 
     * @param wareHourse
     * @return
     */
    public String wareHourseDesc(String wareHourse) {
        String[] wareHourseStrings = StringUtils.split(wareHourse, ",");
        List<WareHourse> wareHourses = this.getWareHourse();
        
        Map<String, WareHourse> wareHoursesMap = CollectionUtils.constructMapValue(wareHourses, "id");
        List<String> result = new ArrayList<String>();
        
        for(String wareHourseString: wareHourseStrings) {
            if(wareHoursesMap.get(wareHourseString) != null) {
                result.add(wareHoursesMap.get(wareHourseString).getName());
            }
        }
        
        return StringUtils.join(result, "、");
    }
    
    /**
     * @return
     */
    public List<VehicleFeature> getVehicleFeatures() {
        return vehicleFeatureService.listAll();
    }
    
    /**
     * 
     * @return
     */
    public List<Team> getTeams() {
        return teamService.listAll(Team.class);
    }
    
    /**
     * @return
     */
    public List<Organization> getSystemOrganization() {
        return organizationService.listSystemOrganization();
    }
    
    /**
     * @return
     */
    @Cacheable("users")
    public List<User> getUsers() {
        return userService.list();
    }
    
}
